<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="类型：">
          <el-select v-model="listPram.type" placeholder="请选择">
            <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.id" class="selWidth">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="openEdit()"
                     v-has-permi="['admin:image:save']">添加
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" size="mini"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column label="ID" prop="id" width="120"></el-table-column>
        <el-table-column label="图片" prop="bannerUrl" width="150">
          <template slot-scope="scope">
            <el-image
                referrerpolicy="no-referrer"
                style="width: 66px!important; height: 66px!important;"
                :src="scope.row.bannerUrl"
                :preview-src-list="[scope.row.bannerUrl]"
            />
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="name" width="130"/>
        <el-table-column label="位置" prop="typeText" min-width="100"/>
        <el-table-column label="类型" prop="classificationText" min-width="100"/>
        <el-table-column label="排序" prop="sort" min-width="100"/>
        <el-table-column label="创建时间" prop="createTime" min-width="150"/>
        <el-table-column label="更新时间" prop="updateTime" min-width="150">
        </el-table-column>
        <el-table-column label="操作" min-width="180" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" size="mini" type="primary" @click="openEdit(scope.row.id)"
                       v-has-permi="['admin:image:save']">编辑
            </el-button>
            <el-button icon="el-icon-delete" size="mini" type="danger" @click="handlerDelete(scope.row.id)"
                       v-has-permi="['admin:image:delete']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :visible.sync="editDialog.show"
        :title="editDialog.id? '编辑':'新增'"
        destroy-on-close
        :close-on-click-modal="false"
        width="800px">
      <edit
          v-if="editDialog.show"
          :id="editDialog.id"
          @hideEditDialog="hideEditDialog"
          ref="editForm"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit'
import {deleteRow, list} from "@/api/image";
import {checkPermi} from "@/utils/permission";
import {sysCategoryList} from "@/api/menu";

export default {
  components: {edit},
  data() {
    return {
      listData: {list: [], count: 0},
      listPram: {
        type: '',
        page: 1,
        limit: 20,
      },
      editDialog: {
        show: false,
        id: null
      },
      tableLoading: false,
      typeList: [
          {id: 1, name: '首页轮播图'},
        {id: 2, name: '首页广告位'},
        {id: 3, name: '活动轮播图'},
        {id: 4, name: '活动板块图'},
        {id: 5, name: '活动板块图'},
        {id: 6, name: '活动板块详情图'},
      ],
      imageUrl: ''
    }
  },
  mounted() {
    this.imageUrl = process.env.VUE_APP_IMAGE_URL
    this.getList()
    // this.getManagementList()
  },
  methods: {
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        res.list.map(item=>{
          item.bannerUrl=this.imageUrl+item.bannerUrl
        })
        this.listData = res;
      }).finally(() => {
        this.tableLoading = false;
      })
    },
    // getManagementList() {
    //   sysCategoryList({code: 'BANNER_TYPE'}).then(res => {
    //     this.typeList = res
    //   })
    // },
    openEdit(id = null) {
      this.editDialog.id = id;
      this.editDialog.show = true
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    resetQuery() {
      this.listPram.type = '';
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
